package com.cly.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.domain.SmsLog;
import com.cly.mapper.SmsLogMapper;
import com.cly.model.SmsModel;
import com.cly.service.SmsLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService{

    @Autowired
    private SmsLogMapper smsLogMapper;

    /**
     * 记录已发送的数据
     *
     * @param userId
     * @param resultCode
     * @param smsModel
     */
    @Override
    public void saveSmsLog(String userId, String resultCode, SmsModel smsModel) {
        log.info("记录发送短信的数据库，手机号{}", smsModel.getPhoneNumbers());
        String templateParam = smsModel.getTemplateParam();
        JSONObject jsonObject = JSON.parseObject(templateParam);
        String code = jsonObject.getString("code");
        SmsLog smsLog = SmsLog.builder()
                .userId(userId)
                .userPhone(smsModel.getPhoneNumbers())
                .recDate(new Date())
                .status(1)
                .type(2)
                .content("尊敬的用户，您的注册会员动态密码为：" + code + "，请勿泄漏于他人")
                .mobileCode(code)
                .responseCode(resultCode)
                .build();
        smsLogMapper.insert(smsLog);
    }
}
